<?php
/**
 * @file submit.php 提交小说下载地址等....
 * @package LD-liewz.com
 * @subpackage 
 * @version $id: 0.1 UTF-8 2009-1-24 下午04:11:27 $
 * @author LD King (mifunny.info)
 * @copyright Copyright (c) 2007 - 2008 MiFunny China Inc. (http://mifunny.info)
 */
	$submit = $_POST['submit'];
	if (!empty($submit)) {
	$postID = $_POST['postID'];  //文章编号
	$postMeta = array();
	$postMeta['title'] = $_POST['title'];  //书名
	$postMeta['author'] = $_POST['author']; //作者
	$postMeta['url_es'] = $_POST['url_es']; //
	$postMeta['url_ziddu'] = $_POST['url_ziddu']; //
	$postMeta['url_rayfile'] = $_POST['url_rayfile']; //
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>提交小说meta信息 | 猎物者</title> 

<style type="text/css">
*{margin: 10px;}
#page{ width:800px; margin:20px 10px 10px 50px;font-size:16px;}
#page textarea{width:100%;height:40px;}
#page input{height:18px;font-size:16px;}
.error{color:red;font-size:16px;}
.box{margin:5px;border:2px solid #f2f2cd;background-color:#d3f2cd;}
.box td{border-top:1px solid #343434;padding:5px 2px 2px 5px;}
</style>

</head>
<body>

<div id="page">
<p>输入小说的信息：<span class="error">必需输入 “编号” ！</span></p>
<form action="submit.php" method="post" target="_top">
编号：<input type="text" name="postID" /><br />
书名：<input type="text" name="title" /><br />
作者：<input type="text" name="author" /><br />

EasyShare地址：<textarea name="url_es" rows="2" cols="45"></textarea><br />
Ziddu地址：<textarea name="url_ziddu" rows="2" cols="45"></textarea><br />
Rayfile地址：<textarea name="url_rayfile" rows="2" cols="45"></textarea><br />

<input type="submit" name="submit" value="提交" /> | 
<input type="reset" name="reset" value="重置" />
</form>

<?php 
// 没有输入编号
if (!empty($submit) and empty($postID)){
	die("<span class=\"error\">没有输入“编号”</span>, <a href=\"submit.php\">返回原页</a>");
}
?>

<?php 
if (!empty($submit)){
define('DB_HOST', 'localhost');
define('DB_USER', 'mifunny');
define('DB_PASSWORD', 'mifunny');
define('DB_NAME', 'liewz');
define('DB_CHARSET', 'utf8');

$wpdb = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
    echo "Connect failed: %s<br />".mysqli_connect_error();
    exit();
}
$wpdb->query("SET NAMES ".DB_CHARSET);

foreach ($postMeta as $key => $value){
	if (empty($value)){
		continue;
	} // END if 空则跳过
	$sqlSelect = "SELECT `meta_id` FROM `".DB_NAME."`.`wp_postmeta` 
		WHERE `post_id` =".$postID." AND `meta_key` = \"".$key."\" LIMIT 1";
	$metaID = $wpdb->query($sqlSelect)->fetch_object()->meta_id;
	
	// 选择插入或者更新信息
	if (empty($metaID)){
		$sqlInsert = "INSERT INTO `".DB_NAME."`.`wp_postmeta` 
			(`meta_id` ,`post_id` ,`meta_key` ,`meta_value`)
			VALUES ( NULL , '".$postID."', '".$key."', '".$value."');";
		$wpdb->query($sqlInsert);
		var_dump($sqlInsert);
	}else {
		$sqlUpdate = "UPDATE `".DB_NAME."`.`wp_postmeta`
			 SET `meta_value` = '".$value."' 
			 WHERE `wp_postmeta`.`meta_id` =".$metaID." LIMIT 1 ;";
		$wpdb->query($sqlUpdate);
	}
} //END foreach

$sqlSelect = " SELECT `meta_id` , `meta_key` , `meta_value` 
		FROM `".DB_NAME."`.`wp_postmeta` WHERE `post_id` = ".$postID." LIMIT 0 , 30 ";
$resultSelect = $wpdb->query($sqlSelect);
?>
<table class="box"><tr><th>meta_id</th><th>meta_key</th><th>meta_value</th></tr>
<?php 
while ($row = $resultSelect->fetch_assoc()){
	if ($row['meta_key'] == '_edit_lock' or $row['meta_key'] == '_edit_last')
		continue;
	echo "<tr><td>".$row['meta_id']."</td><td>".$row['meta_key']."</td><td>".$row['meta_value']."</td></tr>";	
}
?>
</table>
<?php 
$resultSelect->close();
$wpdb->close();
unset($submit);
} // END if submit非空
?>
</div><!-- #page -->
</body>
</html>